package com.comment.project.oss;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.CannedAccessControlList;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.Date;

/**
 * @author liupenghui
 * @date 2018/7/26
 * @desc：
 */
public class Test {
    public static void main(String[] args) throws FileNotFoundException {
// Endpoint以杭州为例，其它Region请按实际情况填写。
        String endpoint = "http://oss-cn-beijing.aliyuncs.com";
// 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
        String accessKeyId = "LTAIv2HaWLvh3TUB";
        String accessKeySecret = "HtfWNa1LO2ZI2aYqZ7OQ073zeah2rO";
        String bucketName = "skrskr";
        String objectName = "gou.jpg";
// 创建OSSClient实例。
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        // 上传文件。
//        String content = "Hello OSS";
//        File file = new File("C:\\Users\\Administrator\\Desktop\\gou.jpg");
//        ossClient.putObject(bucketName, objectName, file);
// 关闭OSSClient。
///        PutObjectResult result = ossClient.putObject(bucketName, file.getName(), new ByteArrayInputStream(content.getBytes()));



//        PutObjectResult result = ossClient.putObject(bucketName, file.getName(), new FileInputStream(file));

        Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 10);
//        // 生成URL
        URL url = ossClient.generatePresignedUrl(bucketName, objectName,expiration);

        System.out.println(url.toString());
        // 关闭OSSClient。
        ossClient.shutdown();
    }

}
